tongfusoft.folders = function() {
}

tongfusoft.folders.prototype = {
	foldersList : function() {
		common.post('folders/list', {}, function(data) {
			if (data.state == 'success') {

				var defaultData = [ {
					text : data.data.text,
					href : data.data.href,
					tags : data.data.tags,
					nodes : data.data.nodes
				} ];

				var defaultDataee = eval(defaultData)

				$('#treeview2').treeview(
						{
							levels : 99,
							data : defaultDataee,
							showTags : true,
							onNodeSelected : function(event, node) {
								
								$("#folderId").val(node.tags[0]);
								$("#currentName").val(node.text);
								var parentName = $('#treeview2').treeview('getParent', node).text;					
								var nodes = node.nodes.length;
								$("#nodesNum").val(nodes);
								$("#nodeId").val(node.nodeId);
								if (node.nodeId == '0') {
									$("#parentName").val("");
								} else {
									$("#parentName").val($('#treeview2').treeview('getParent', node).text);
								}
								if($("#opr").val() === 'add'){
									new tongfusoft.folders().add();
								} else if($("#opr").val() === 'edit'){
									new tongfusoft.folders().edit();
								}
								
							},
							onNodeUnselected : function(event, node) {
								$("#folderId").val("");
								$("#nodeId").val("");
								$("#parentName").val("");
								$("#currentName").val("");
								$("#foldersUp").hide();

							}
						});
			}
		});
	},

	add : function() {
		var folderId = $("#folderId").val();
		if (folderId != '') {
			var currentName = $("#currentName").val();
			$("#rootFolderName").text(currentName);
			$("#opr").val("add");
			$("#foldersUp").show();
			$("#folderName").val("");
			$("#title").text("添加文件夹");
			$("#currFolerName").text("当前文件夹：");
			$("#newFolerName").text("*新建文件夹：");
		} else {
			$("#foldersUp").hide();
			toastr["error"]('请选择文件夹!');
		}
	},
	// 提交数据
	dataSubmit : function() {

		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("foldersForm");
				initData['folderId'] = $("#folderId").val();
				if ($("#opr").val() === 'add') {
					var url = 'folders/create';
				} else if ($("#opr").val() === 'edit') {
					var url = 'folders/update';
				}
						
				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {
						new tongfusoft.folders().foldersList();
						$("#foldersUp").hide();
						$("#opr").val('');
						$("#folderId").val("");
						$("#parentName").val("");
						$("#currentName").val("");
						toastr["success"]('保存成功!');
					} else if(data.code=='repeat_error'){
						
						toastr["error"]('该文件夹已存在，请重新输入!');
					} else if(data.state=='errornull'){
						
						toastr["error"]('文件夹不能为空!');
					}else {
						toastr["error"]('保存失败!');
					}
				}, $("#oper").val())
			}
		});
		$("#foldersForm").validate();

		$("#submitBtn").click(function() {
			$("#foldersForm").submit();
		})
	},
	
	edit : function() {
		var folderId = $("#folderId").val();
		
		if (folderId != '') {
			var parentName = $("#parentName").val();
			if($("#nodeId").val()=== '0'){
				toastr["error"]('根目录不允许修改!');
			}else{
				$("#rootFolderName").text(parentName);
				$("#folderName").val($("#currentName").val());
				$("#opr").val("edit");
				$("#foldersUp").show();
				$("#currFolerName").text("父级文件夹：");
				$("#newFolerName").text("当前文件夹：");
				$("#title").text("修改文件夹");
			}
		} else {
			$("#foldersUp").hide();
			toastr["error"]('请选择文件夹!');
		}
	},
	
	// 删除
	del : function(obj) {
		
	    var folderId = $("#folderId").val();
	    
	    if (folderId != '') {
	    	var nodes = $("#nodesNum").val();
	    	if($("#nodeId").val()=== '0'){
    			toastr["error"]('根目录不允许删除!');
    		}else{
    			if(nodes > 0){
    	    		toastr["error"]('该文件夹下有子文件夹不能删除!');
    	    	}else{
    	    		common.post('folders/isHasDocument', {
    	    			"folderId" : folderId
    	    		}, function(data) {
    	    			
    	    			if (data.state == 'success') {
    	    				toastr["error"]('该文件夹下有文档不能删除!');
    	    			}else{
    	    				swal({
    							title : "是否确认?",
    							text : "",
    							type : "warning",
    							showCancelButton : true,
    							confirmButtonColor : "#DD6B55",
    							confirmButtonText : "删除",
    							cancelButtonText : "取消",
    							closeOnConfirm : true
    						}, function() {
    							common.ajaxSubmitOnSuccess({
    								"folderId" : folderId
    							}, 'folders/del', function(data) {

    								if (data.state == 'success') {
    									$("#folderId").val("");
    									$("#parentName").val("");
    									$("#currentName").val("");
    									new tongfusoft.folders().foldersList();
    								}else{
    									toastr["error"]('删除失败!');
    								}

    							}, "del")
    						});
    	    			}
    	    			
    	    		});
    	    		
    	    		
    	    	}
    		}

	    } else {
				toastr["error"]('请选择文件夹!');
		}
	},

}

$(function() {

	var obj = new tongfusoft.folders();

	obj.foldersList();
	obj.dataSubmit();

})